package com.supor.aiot.mine.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.Gravity;

import com.android.baseconfig.common.utils.Util;
import com.supor.aiot.mine.R;

import androidx.annotation.Nullable;

/**
 * ---------------------------------------------------------------- <br>
 * Copyright (C) 2014-2021, by het, ShenZhen, All rights reserved.  <br>
 * ---------------------------------------------------------------- <br>
 * <p>
 * 描述: ArrowTextView <br>
 * 作者: lei <br>
 * 日期: 2021/8/18 <br>
 */
public class ArrowTextView extends androidx.appcompat.widget.AppCompatTextView {
    private final static int TRIANGLE_DIRECTION_TOP = 1;
    private final static int TRIANGLE_DIRECTION_BOTTOM = 2;
    private final static int TRIANGLE_DIRECTION_LEFT = 1;
    private final static int TRIANGLE_DIRECTION_RIGHT = 2;


    private Paint mPaint;
    private Paint mStrokePaint;

    private int mBgColor;
    private int mStrokeColor;
    private int mStrokeWidth;
    private int mTotalHeight;
    private int mTotalWidth;
    private int mLabelHeight;
    private int mTriangleHeight;
    private int mTriangleWidth;
    private int mRadius;
    private int triangleDirection;

    public ArrowTextView(Context context) {
        this(context, null);
    }

    public ArrowTextView(Context context,
                               @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ArrowTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        if (attrs != null) {
            TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ArrowTextView);
            typedArray.recycle();
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ArrowTextView);
            mBgColor = a.getColor(R.styleable.ArrowTextView_bubbleColor, 0);
            mStrokeColor = a.getColor(R.styleable.ArrowTextView_bubbleStrokeColor, 0);
            mRadius = a.getDimensionPixelOffset(R.styleable.ArrowTextView_bubbleRadius, 0);
            mStrokeWidth =
                    a.getDimensionPixelOffset(R.styleable.ArrowTextView_bubbleStrokeWidth, 0);
            mTriangleHeight =
                    a.getDimensionPixelOffset(R.styleable.ArrowTextView_triangleHeight,
                    Util.dip2px(context, 6));
            mTriangleWidth =
                    a.getDimensionPixelOffset(R.styleable.ArrowTextView_triangleWidth,
                            (int) Util.dip2px(context, 3.5f));
            triangleDirection = a.getInt(R.styleable.ArrowTextView_triangleDirection, 0);
            a.recycle();
        }

        setGravity(Gravity.CENTER);
        initPaint();
    }

    //初始化画笔
    public void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.FILL);
        mPaint.setTextSize(getPaint().getTextSize());
        mPaint.setDither(true);
    }

    //初始化边框线画笔
    public void initStrokePaint() {
        mStrokePaint = new Paint();
        mStrokePaint.setAntiAlias(true);
        mStrokePaint.setStyle(Paint.Style.FILL);
        mStrokePaint.setDither(true);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mLabelHeight = getFontHeight() + getPaddingTop() + getPaddingBottom();
        mTotalHeight = mLabelHeight + mTriangleHeight * 2 + mStrokeWidth * 2;
        mTotalWidth = getPaddingLeft() + getFontWidth() + getPaddingRight() + mStrokeWidth * 2;
        setMeasuredDimension(mTotalWidth, mTotalHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawView(canvas);
        super.onDraw(canvas);
    }

    //绘制气泡
    private void drawView(Canvas canvas) {
        if (mStrokeColor != 0 && mStrokeWidth != 0) {
            initStrokePaint();
            mStrokePaint.setColor(mStrokeColor);
            drawRound(canvas, mStrokePaint, 0);
            drawTriangle(canvas, mStrokePaint, 0);
        }
        if (mBgColor != 0) {
            mPaint.setColor(mBgColor);
            drawRound(canvas, mPaint, mStrokeWidth);
            drawTriangle(canvas, mPaint, mStrokeWidth);
        }
    }

    //绘制矩形
    private void drawRound(Canvas canvas, Paint paint, int strokeWidth) {
        canvas.drawRoundRect(strokeWidth, mTriangleHeight + strokeWidth,
                mTotalWidth - strokeWidth, mTotalHeight - mTriangleHeight - strokeWidth,
                mRadius, mRadius, paint);
    }

    //绘制三角形
    private void drawTriangle(Canvas canvas, Paint paint, int strokeWidth) {
        Path path = new Path();
        switch (triangleDirection) {
            //上
            case TRIANGLE_DIRECTION_TOP:
                path.moveTo(mTotalWidth * 0.2f - mTriangleWidth / 2 + strokeWidth / 2,
                        mTriangleHeight + strokeWidth);
                path.lineTo(mTotalWidth * 0.2f, strokeWidth + strokeWidth / 2);
                path.lineTo(mTotalWidth * 0.2f + mTriangleWidth / 2 - strokeWidth / 2,
                        mTriangleHeight + strokeWidth);
                break;
            //下
            case TRIANGLE_DIRECTION_BOTTOM:
                path.moveTo(mTotalWidth * 0.8f - mTriangleWidth / 2 + strokeWidth / 2,
                        mTotalHeight - mTriangleHeight
                        - strokeWidth);
                path.lineTo(mTotalWidth * 0.8f, mTotalHeight - strokeWidth - strokeWidth / 2);
                path.lineTo(mTotalWidth * 0.8f + mTriangleWidth / 2 - strokeWidth / 2,
                        mTotalHeight - mTriangleHeight
                        - strokeWidth);
                break;
            default:
                return;
        }
        canvas.drawPath(path, paint);
    }

    //根据字号求字体高度
    private int getFontHeight() {
        Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
        return Math.round(fontMetrics.descent - fontMetrics.ascent);
    }

    //根据字号求字体宽度
    private int getFontWidth() {
        return (int) mPaint.measureText(getText().toString());
    }

}
